import hashlib
import random
import json

ALL_CHARS = '0123456798qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'


# random.choice 随机取数,有重复
def random_captcha_text(length=4):
    """生成随机验证码字符串"""
    return ''.join(random.sample(ALL_CHARS, k=length))


def random_mobile_code(length=6):
    """生成随机短信验证码"""
    code = ''
    for _ in range(length):
        code += random.sample('0123456789', k=1)[0]
    return code


def to_md5_hex(content):
    """生产字符串的MD5摘要"""
    return hashlib.md5(content.encode()).hexdigest()


import requests


def send_messag_example(tel, code):
    resp = requests.post("http://sms-api.luosimao.com/v1/send.json",
                         auth=("api", "key-f592f7746db59e7471e5e87c041f4b12"),
                         data={
                             "mobile": "%d" % tel,
                             "message": "你的验证码是%s【铁壳测试】" % code
                         }, timeout=3, verify=False)
    result = json.loads(resp.content)
    return result


# if __name__ == "__main__":
#     print(type(random_mobile_code()))
#     print(len(random_mobile_code()))

